package com.icss.ah.base.util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class XmlUtils {
	public static void main(String [] args) {
		
		StringBuffer retValue = new StringBuffer();
		retValue.append("<?xml version='1.0' encoding='utf-8'?>");
		retValue.append("<DATASETS>");
		retValue.append("  <DATASET>");
		retValue.append("    <VIS_DATE>2013-05-07</VIS_DATE>");
		retValue.append("    <CLIENT_CODE>0613418017203</CLIENT_CODE>");
		retValue.append("  </DATASET>");
		retValue.append("</DATASETS>");
		
		try {
			List listHead = xmlElements(retValue.toString());
			System.out.println("---------listHead--------------"+listHead.size());
			/*HashMap map = (HashMap)listHead.get(0); 
			Iterator ite=map.entrySet().iterator();
			while(ite.hasNext()){
		         Map.Entry entry=(Map.Entry)ite.next();
		         System.out.print("-------------------"+entry.getKey()+"/");
		            System.out.println("-------------------"+entry.getValue());
		    }
			System.out.println("---------map--size:"+map.size()+";"); */
			//List list = getReceiveXmlList(info);
			for(int i=0 ;i<listHead.size();i++){
				Map mp = (Map) listHead.get(i);
				Iterator ite=mp.entrySet().iterator();
				while(ite.hasNext()){
			         Map.Entry entry=(Map.Entry)ite.next();
			         System.out.print("======="+entry.getKey()+"/");
			            System.out.println(entry.getValue());
			    }
				System.out.println("------------size:"+mp.size()+";i:"+i); 
			} 
			//String phDate="20120424";
			//phDate = phDate.substring(0, 4)+"-"+phDate.substring(4, 6)+"-"+phDate.substring(6, 8);
			
			//System.out.println("------------phDate----------"+phDate); 
			
		}catch(Exception e){
			e.printStackTrace();
		}
	 	
	}
	public static List xmlElements(String xmlDoc) {
		List list = new ArrayList();
        try {
        	if(xmlDoc!=null && xmlDoc.trim().length()>0){
	    		xmlDoc = xmlDoc.replaceAll("&","&amp;");
	            StringReader read = new StringReader(xmlDoc);
	            InputSource source = new InputSource(read);
	            SAXBuilder sb = new SAXBuilder();
	            Document doc = sb.build(source);
	            Element root = doc.getRootElement();
	            List jiedian = root.getChildren();
	            Element et = null;
	            for(int i=0;i<jiedian.size();i++){
	                et = (Element) jiedian.get(i);
	                List attrs = et.getChildren();
	                Map hm = new HashMap();
	                for(int j=0;j<attrs.size();j++){
	                	Element att =(Element)attrs.get(j);
	                	String key = att.getName();
	                	if(key!=null && !key.equals("")){
	                		key = key.toUpperCase();
	                	}
	                	String value = att.getText();
	                	hm.put(key,value);
	                }
	                list.add(hm);
	            }
        	}
        }catch(Exception ex) {
        	ex.printStackTrace();
        }
        return list;
    }
	public static List xmlElementsTS(String xmlDoc) {
		List list = new ArrayList();
        try {
        	if(xmlDoc!=null && xmlDoc.trim().length()>0){
	    		xmlDoc = xmlDoc.replaceAll("&","&amp;");
	            StringReader read = new StringReader(xmlDoc);
	            InputSource source = new InputSource(read);
	            SAXBuilder sb = new SAXBuilder();
	            Document doc = sb.build(source);
	            Element root = doc.getRootElement();
	            List jiedian = root.getChildren();
	            Element et = null;
	            for(int i=0;i<jiedian.size();i++){
	                et = (Element) jiedian.get(i);
	                List attrs = et.getChildren();
	                Map hm = new HashMap();
	                for(int j=0;j<attrs.size();j++){
	                	Element att =(Element)attrs.get(j);
	                	String key = att.getName();
	                	if(key!=null && !key.equals("")){
	                		key = key.toUpperCase();
	                	}
	                	String value = att.getText();
	                	if(!"TIMUS".equals(key)){
	                		hm.put(key,value);
	                	}else{
	                		List attrss = att.getChildren();
	                		if(attrss==null||attrss.size()==0){
	                			hm.put("ID", "empty");
	                			list.add(hm);
	                		} else {
	                			for(int n=0;n<attrss.size();n++){
	                				Element att1 =(Element)attrss.get(n);
	                				
	                				Map hm1 = new HashMap();
	                				hm1.putAll(hm);
	                				
	                				String id = att1.getAttributeValue("id");
	                				String type = att1.getAttributeValue("type");
	                				String values = att1.getAttributeValue("value");
	                				hm1.put("ID", id);
	                				hm1.put("TYPE", type);
	                				hm1.put("VALUE", values);
	                				list.add(hm1);
	                			}
	                		}
	                	}
	                }
	            }
        	}
        }catch(Exception ex) {
        	ex.printStackTrace();
        }
        return list;
    }

}
